package com.ld.control.business.record.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.record.mapper.MessageNoticeRecordMapper;
import com.ld.control.business.record.model.MessageNoticeRecordModel;
import com.ld.control.business.record.service.ErrorNoticeRecordService;
import com.ld.control.business.record.service.MessageNoticeRecordService;
import com.ld.control.business.record.vo.req.ReadAlreadyReqVo;
import com.ld.control.business.record.vo.resp.MessageNoticeRecordRespVo;
import com.ld.control.business.record.vo.resp.NoticeTipRespVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.sysenum.ErrorNoticeRecordStatusEnum;
import com.ld.control.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * <p>
 * 任务报错后的记录和处理 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-07-15
 */
@Service
public class MessageNoticeRecordServiceImpl extends ServiceImpl<MessageNoticeRecordMapper, MessageNoticeRecordModel> implements MessageNoticeRecordService {

    @Autowired
    private MessageNoticeRecordMapper messageNoticeRecordMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private ErrorNoticeRecordService errorNoticeRecordService;

    @Override
    public void add(String msgInfo) {
        MessageNoticeRecordModel messageNoticeRecordModel = new MessageNoticeRecordModel();
        messageNoticeRecordModel.setMsgInfo(msgInfo);
        messageNoticeRecordModel.setStatus(0);
        messageNoticeRecordModel.setCreateTime(new Date());
        messageNoticeRecordMapper.insert(messageNoticeRecordModel);
    }

    @Override
    public GeneratePageListVo<MessageNoticeRecordRespVo> getPageList(Integer status, Integer type, Integer current, Integer size) {
        IPage<MessageNoticeRecordRespVo> iPage = new Page<>(current, size);
        IPage<MessageNoticeRecordRespVo> iPage1 = messageNoticeRecordMapper.selectPageList(iPage, status, type);
        return new GeneratePageListVo<>(iPage1);
    }

    @Override
    public void readAlready(ReadAlreadyReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        if (ObjectUtils.isEmpty(reqVo)) {
            messageNoticeRecordMapper.updateStatusToRead(currentUserAccountId);
            return;
        }
        for (Long id : reqVo.getIdList()) {
            MessageNoticeRecordModel messageNoticeRecordModel = messageNoticeRecordMapper.selectById(id);
            messageNoticeRecordModel.setId(id);
            messageNoticeRecordModel.setStatus(1);
            messageNoticeRecordModel.setUpdateBy(currentUserAccountId);
            messageNoticeRecordModel.setUpdateTime(new Date());
            messageNoticeRecordMapper.updateById(messageNoticeRecordModel);
        }
    }

    @Override
    public NoticeTipRespVo getNoticeTipInfo() {
        Integer errorCount = errorNoticeRecordService.getErrorCount();
        Integer noticeTipCount = getNoticeTipCount();
        NoticeTipRespVo noticeTipRespVo = new NoticeTipRespVo();
        noticeTipRespVo.setNoticeCount(noticeTipCount);
        noticeTipRespVo.setWarnCount(0);
        noticeTipRespVo.setErrorCount(errorCount);
        if (errorCount != 0 || noticeTipCount != 0) {
            noticeTipRespVo.setNoticeTipFlag(1);
        } else {
            noticeTipRespVo.setNoticeTipFlag(0);
        }
        return noticeTipRespVo;
    }

    @Override
    public Integer getNoticeTipCount() {
        QueryWrapper<MessageNoticeRecordModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(MessageNoticeRecordModel.STATUS, ErrorNoticeRecordStatusEnum.UNPROCESSED);
        Long count = messageNoticeRecordMapper.selectCount(queryWrapper);
        return count.intValue();
    }
}
